package com.xiaoma.chat.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class IMDateUtils {

    //获取指定日期的前一天
    //eg:20150601 or 2015060109
    public static String getSpecifiedDayBefore(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay.substring(0, 8));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyyMMdd").format(c.getTime());
        return dayBefore;
    }
    
    public static String getSpecifiedDayAfter(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay.substring(0, 8));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayBefore = new SimpleDateFormat("yyyyMMdd").format(c.getTime());
        return dayBefore;
    }
    
    public static String getFormatedDate(Date date,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    
    public static boolean isBeforNow(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowStr = sdf.format(new Date());
        dateStr = dateStr.substring(0,8);
        return Integer.parseInt(nowStr) - Integer.parseInt(dateStr) > 0;
    }
    
    public static void main(String[] args) {
        String s = getSpecifiedDayAfter("20150531");
        System.out.println(s);
        String ss = getFormatedDate(new Date(),"yyyyMMdd");
        System.out.println(ss);
        System.out.println(isBeforNow("2015072201"));
    }
}
